package main

import (
	"fmt"
	"github.com/boltdb/bolt"
	"kongyixueyuan.com/MyPublicChain/part14-block-boltdb/BLC"
	"log"
)

func main() {

	//block := BLC.NewBlock("Test l",1,[]byte{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0})


	// 1、创建数据库
	db, err := bolt.Open("my.db",0600,nil)
	if err != nil{
		log.Fatal(err)
	}
	defer db.Close()

	// 更新数据库
	/*err = db.Update(func(tx *bolt.Tx) error {
		// 取表对象
		b := tx.Bucket([]byte("blocks"))

		if b == nil {
			b , err = tx.CreateBucket([]byte("blocks"))
			if err != nil {
				log.Panic("创建表失败")
			}
		}
		b.Put([]byte("l"),block.SeriaLize())
		if err != nil {
			log.Panic(err)
		}

		return nil
	})

	if err != nil {
		log.Panic(err)
	}*/


	// 查询数据库
	err = db.View(func(tx *bolt.Tx) error {

		b :=tx.Bucket([]byte("blocks"))
		if b != nil {
			blockData := b.Get([]byte("l"))
			block := BLC.DeserializeBlock(blockData)
			fmt.Printf("%v\n",block)
		}

		return nil
	})

	if err != nil {
		log.Panic(err)
	}
}
